package com.ruoyi.team.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.team.domain.Goods;
import com.ruoyi.team.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.team.mapper.StockBillItemMapper;
import com.ruoyi.team.domain.StockBillItem;
import com.ruoyi.team.service.IStockBillItemService;

/**
 * 出入库单据明细Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-28
 */
@Service
public class StockBillItemServiceImpl extends ServiceImpl<StockBillItemMapper, StockBillItem> implements IStockBillItemService {


    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private StockBillItemMapper stockBillItemMapper;
    /**
     * 查询出入库单据明细列表
     *
     * @param stockBillItem 出入库单据明细
     * @return 出入库单据明细
     */
    @Override
    public List<StockBillItem> selectStockBillItemList(StockBillItem stockBillItem) {

        List<StockBillItem> stockBillItemList = getBaseMapper().selectStockBillItemList(stockBillItem);

        for (StockBillItem billItem : stockBillItemList) {

            // 通过商品 id 查询商品信息
            Goods good = goodsService.getBaseMapper().selectById(billItem.getGoodsId());
            billItem.setGoodName(good.getGoodsName());
        }
        return stockBillItemList;
    }

    @Override
    public List<StockBillItem> selectByStoreId(Long billId) {

        List<StockBillItem> stockBillItems = stockBillItemMapper.selectList(
                new LambdaUpdateWrapper<StockBillItem>()
                        .eq(StockBillItem::getBillId, billId));
        for (StockBillItem stockBillItem : stockBillItems) {
            Goods good = goodsService.getById(stockBillItem.getGoodsId());
            stockBillItem.setGoodName(good.getGoodsName());
        }

        return stockBillItems;
    }
}
